<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>webdoc</title>
    <link rel="stylesheet" type="text/css" href="../static/css/main.css">
</head>
<body>
<div class="nav">
    <div class="logo">
        
            webdoc
        
    </div>
<ul><li><a href="../index.html">0.Async</a></li><li><a href="../html/0.editor.html">0.editor</a></li><li><a href="../html/0.module.html">0.module</a></li><li><a href="../html/1.ES2015.html">1.ES2015</a></li><li><a href="../html/2.Promise.html">2.Promise</a></li><li><a href="../html/3.Node.html">3.Node</a></li><li><a href="../html/4.NodeInstall.html">4.NodeInstall</a></li><li><a href="../html/5.REPL.html">5.REPL</a></li><li><a href="../html/6.NodeCore.html">6.NodeCore</a></li><li><a href="../html/7.module&NPM.html">7.module&NPM</a></li><li><a href="../html/8.Encoding.html">8.Encoding</a></li><li><a href="../html/9.Buffer.html">9.Buffer</a></li><li><a href="../html/10.fs.html">10.fs</a></li><li><a href="../html/11.Stream-1.html">11.Stream-1</a></li><li><a href="../html/11.Stream-2.html">11.Stream-2</a></li><li><a href="../html/11.Stream-3.html">11.Stream-3</a></li><li><a href="../html/11.Stream-4.html">11.Stream-4</a></li><li><a href="../html/12-Network-2.html">12-Network-2</a></li><li><a href="../html/12.NetWork-3.html">12.NetWork-3</a></li><li><a href="../html/12.Network-1.html">12.Network-1</a></li><li><a href="../html/13.tcp.html">13.tcp</a></li><li><a href="../html/14.http-1.html">14.http-1</a></li><li><a href="../html/14.http-2.html">14.http-2</a></li><li><a href="../html/15.compress.html">15.compress</a></li><li><a href="../html/16.crypto.html">16.crypto</a></li><li><a href="../html/17.process.html">17.process</a></li><li><a href="../html/18.yargs.html">18.yargs</a></li><li><a href="../html/19.cache.html">19.cache</a></li><li><a href="../html/20.action.html">20.action</a></li><li><a href="../html/21.https.html">21.https</a></li><li><a href="../html/22.cookie.html">22.cookie</a></li><li><a href="../html/23.session.html">23.session</a></li><li><a href="../html/24.express-1.html">24.express-1</a></li><li><a href="../html/24.express-2.html">24.express-2</a></li><li><a href="../html/24.express-3.html">24.express-3</a></li><li><a href="../html/24.express-4.html">24.express-4</a></li><li><a href="../html/25.koa-1.html">25.koa-1</a></li><li><a href="../html/26.webpack-1-basic.html">26.webpack-1-basic</a></li><li><a href="../html/26.webpack-2-optimize.html">26.webpack-2-optimize</a></li><li><a href="../html/26.webpack-3.tapable.html">26.webpack-3.tapable</a></li><li><a href="../html/26.webpack-4-AST.html">26.webpack-4-AST</a></li><li><a href="../html/26.webpack-5-source.html">26.webpack-5-source</a></li><li><a href="../html/26.webpack-6-loader.html">26.webpack-6-loader</a></li><li><a href="../html/26.webpack-7-plugin.html">26.webpack-7-plugin</a></li><li><a href="../html/26.webpack-8-hand.html">26.webpack-8-hand</a></li><li><a href="../html/27.react-1.html">27.react-1</a></li><li><a href="../html/27.react-2.html">27.react-2</a></li><li><a href="../html/27.react-3.html">27.react-3</a></li><li><a href="../html/27.react-4-immutable.html">27.react-4-immutable</a></li><li><a href="../html/27.react-5-react-dom-diff.html">27.react-5-react-dom-diff</a></li><li><a href="../html/27.react-6.html">27.react-6</a></li><li><a href="../html/28.react-mobx.html">28.react-mobx</a></li><li><a href="../html/28.redux-0.html">28.redux-0</a></li><li><a href="../html/28.redux-1.html">28.redux-1</a></li><li><a href="../html/28.redux-2-中间件.html">28.redux-2-中间件</a></li><li><a href="../html/28.redux-3-saga.html">28.redux-3-saga</a></li><li><a href="../html/28.redux-jwt-back.html">28.redux-jwt-back</a></li><li><a href="../html/28.redux-jwt-front.html">28.redux-jwt-front</a></li><li><a href="../html/29.mongodb-1.html">29.mongodb-1</a></li><li><a href="../html/29.mongodb-2.html">29.mongodb-2</a></li><li><a href="../html/29.mongodb-3.html">29.mongodb-3</a></li><li><a href="../html/29.mongodb-4.html">29.mongodb-4</a></li><li><a href="../html/29.mongodb-5.html">29.mongodb-5</a></li><li><a href="../html/29.mongodb-6.html">29.mongodb-6</a></li><li><a href="../html/30.cms-1-mysql.html">30.cms-1-mysql</a></li><li><a href="../html/30.cms-2-mysql.html">30.cms-2-mysql</a></li><li><a href="../html/30.cms-3-mysql.html">30.cms-3-mysql</a></li><li><a href="../html/30.cms-4-egg.html">30.cms-4-egg</a></li><li><a href="../html/30.cms-5-api.html">30.cms-5-api</a></li><li><a href="../html/30.cms-6-roadhog.html">30.cms-6-roadhog</a></li><li><a href="../html/30.cms-7-umi.html">30.cms-7-umi</a></li><li><a href="../html/30.cms-8-dva.html">30.cms-8-dva</a></li><li><a href="../html/30.cms-9-dva.html">30.cms-9-dva</a></li><li><a href="../html/30.cms-10-dva.html">30.cms-10-dva</a></li><li><a href="../html/30.cms-11-front.html">30.cms-11-front</a></li><li><a href="../html/31.cms-12-api.html">31.cms-12-api</a></li><li><a href="../html/31.cms-13-front.html">31.cms-13-front</a></li><li><a href="../html/31.cms-14-deploy.html">31.cms-14-deploy</a></li><li><a href="../html/32.ant.html">32.ant</a></li><li><a href="../html/33.redis.html">33.redis</a></li><li><a href="../html/34.unittest.html">34.unittest</a></li><li><a href="../html/35.jwt.html">35.jwt</a></li><li><a href="../html/36.websocket-1.html">36.websocket-1</a></li><li><a href="../html/36.websocket-2.html">36.websocket-2</a></li><li><a href="../html/38.chat-api-1.html">38.chat-api-1</a></li><li><a href="../html/38.chat-api-2.html">38.chat-api-2</a></li><li><a href="../html/38.chat-3.html">38.chat-3</a></li><li><a href="../html/38.chat-api-3.html">38.chat-api-3</a></li><li><a href="../html/38.chat.html">38.chat</a></li><li><a href="../html/38.chat2.html">38.chat2</a></li><li><a href="../html/38.chat2.html">38.chat2</a></li><li><a href="../html/39.crawl-0.html">39.crawl-0</a></li><li><a href="../html/39.crawl-1.html">39.crawl-1</a></li><li><a href="../html/39.crawl-2.html">39.crawl-2</a></li><li><a href="../html/40.deploy.html">40.deploy</a></li><li><a href="../html/41.safe.html">41.safe</a></li><li><a href="../html/42.test.html">42.test</a></li><li><a href="../html/43.nginx.html">43.nginx</a></li><li><a href="../html/44.enzyme.html">44.enzyme</a></li><li><a href="../html/45.docker.html">45.docker</a></li><li><a href="../html/46.elastic.html">46.elastic</a></li><li><a href="../html/47.oauth.html">47.oauth</a></li><li><a href="../html/48.wxpay.html">48.wxpay</a></li><li class="active"><a href="../html/49.nunjucks.html">49.nunjucks</a></li><li><a href="../html/50.ketang.html">50.ketang</a></li><li><a href="../html/index.html">index</a></li><li><a href="../html/51.typescript.html">51.typescript</a></li><li><a href="../html/52.UML.html">52.UML</a></li><li><a href="../html/53.design.html">53.design</a></li><li><a href="../html/index.html">index</a></li></ul></div>


<div class="warpper">

    <div class="page-toc">
        <ul><li><a href="#t01. nunjucks">1. nunjucks</a></li><li><a href="#t12. 安装">2. 安装</a></li><li><a href="#t23. 使用">3. 使用</a><ul><li><a href="#t33.1 渲染字符串">3.1 渲染字符串</a></li><li><a href="#t43.2 渲染文件">3.2 渲染文件</a></li><li><a href="#t53.3 express">3.3 express</a></li></ul></li><li><a href="#t64.语法">4.语法</a><ul><li><a href="#t74.1 变量">4.1 变量</a></li><li><a href="#t84.2 过滤器">4.2 过滤器</a></li><li><a href="#t94.3 if">4.3 if</a></li><li><a href="#t104.4 for">4.4 for</a></li><li><a href="#t114.5 模板继承">4.5 模板继承</a><ul><li><a href="#t124.5.1 index.js">4.5.1 index.js</a></li><li><a href="#t134.5.2 layout.html">4.5.2 layout.html</a></li><li><a href="#t144.5.3 login.html">4.5.3 login.html</a></li></ul></li><li><a href="#t154.6. 包含">4.6. 包含</a><ul><li><a href="#t164.6.1 index.js">4.6.1 index.js</a></li></ul></li></ul></li><li><a href="#t17参考资料">参考资料</a></li></ul>
    </div>
    
    <div class="content markdown-body">
        <h2 id="t01. nunjucks">1. nunjucks <a href="#t01. nunjucks"> # </a></h2>
<p>Nunjucks&#x662F;Mozilla&#x5F00;&#x53D1;&#x7684;&#x4E00;&#x4E2A;&#x7EAF;JavaScript&#x7F16;&#x5199;&#x7684;&#x6A21;&#x677F;&#x5F15;&#x64CE;&#xFF0C;&#x65E2;&#x53EF;&#x4EE5;&#x7528;&#x5728;Node&#x73AF;&#x5883;&#x4E0B;&#xFF0C;&#x53C8;&#x53EF;&#x4EE5;&#x8FD0;&#x884C;&#x5728;&#x6D4F;&#x89C8;&#x5668;&#x7AEF;</p>
<h2 id="t12. &#x5B89;&#x88C5;">2. &#x5B89;&#x88C5; <a href="#t12. &#x5B89;&#x88C5;"> # </a></h2>
<pre><code class="lang-js">npm install nunjucks
</code></pre>
<h2 id="t23. &#x4F7F;&#x7528;">3. &#x4F7F;&#x7528; <a href="#t23. &#x4F7F;&#x7528;"> # </a></h2>
<h3 id="t33.1 &#x6E32;&#x67D3;&#x5B57;&#x7B26;&#x4E32;">3.1 &#x6E32;&#x67D3;&#x5B57;&#x7B26;&#x4E32; <a href="#t33.1 &#x6E32;&#x67D3;&#x5B57;&#x7B26;&#x4E32;"> # </a></h3>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> nunjucks=<span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;nunjucks&apos;</span>);
nunjucks.configure({<span class="hljs-attr">autoescape</span>: <span class="hljs-literal">true</span>});
<span class="hljs-keyword">let</span> ret = nunjucks.renderString(<span class="hljs-string">&apos;hello {{username}}&apos;</span>,{<span class="hljs-attr">username</span>: <span class="hljs-string">&apos;zfpx&apos;</span>});
<span class="hljs-built_in">console</span>.log(ret);
</code></pre>
<h3 id="t43.2 &#x6E32;&#x67D3;&#x6587;&#x4EF6;">3.2 &#x6E32;&#x67D3;&#x6587;&#x4EF6; <a href="#t43.2 &#x6E32;&#x67D3;&#x6587;&#x4EF6;"> # </a></h3>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> nunjucks=<span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;nunjucks&apos;</span>);
nunjucks.configure(<span class="hljs-string">&apos;views&apos;</span>,{<span class="hljs-attr">autoescape</span>:<span class="hljs-literal">true</span>});
<span class="hljs-keyword">let</span> ret2 = nunjucks.render(<span class="hljs-string">&apos;index.html&apos;</span>,{<span class="hljs-attr">name</span>: <span class="hljs-string">&apos;zfpx&apos;</span>});
<span class="hljs-built_in">console</span>.log(ret2);
</code></pre>
<h3 id="t53.3 express">3.3 express <a href="#t53.3 express"> # </a></h3>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> express=<span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;express&apos;</span>);
<span class="hljs-keyword">const</span> nunjucks=<span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;nunjucks&apos;</span>);
<span class="hljs-keyword">const</span> path=<span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;path&apos;</span>);
<span class="hljs-keyword">let</span> app=express();
nunjucks.configure(path.resolve(__dirname,<span class="hljs-string">&apos;views&apos;</span>),{
    <span class="hljs-attr">autoescape</span>: <span class="hljs-literal">true</span>,
    <span class="hljs-attr">express</span>:app
});

app.get(<span class="hljs-string">&apos;/&apos;</span>,<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req,res</span>) </span>{
    res.render(<span class="hljs-string">&apos;index.html&apos;</span>,{<span class="hljs-attr">name</span>:<span class="hljs-string">&apos;zfpx&apos;</span>});
});
app.listen(<span class="hljs-number">8080</span>);
</code></pre>
<h2 id="t64.&#x8BED;&#x6CD5;">4.&#x8BED;&#x6CD5; <a href="#t64.&#x8BED;&#x6CD5;"> # </a></h2>
<h3 id="t74.1 &#x53D8;&#x91CF;">4.1 &#x53D8;&#x91CF; <a href="#t74.1 &#x53D8;&#x91CF;"> # </a></h3>
<p>&#x53D8;&#x91CF;&#x4F1A;&#x4ECE;&#x6A21;&#x677F;&#x4E0A;&#x4E0B;&#x6587;&#x83B7;&#x53D6;&#xFF0C;&#x5982;&#x679C;&#x4F60;&#x60F3;&#x663E;&#x793A;&#x4E00;&#x4E2A;&#x53D8;&#x91CF;&#x53EF;&#x4EE5;&#xFF1A;</p>
<pre><code class="lang-js">{{ username }}
</code></pre>
<h3 id="t84.2 &#x8FC7;&#x6EE4;&#x5668;">4.2 &#x8FC7;&#x6EE4;&#x5668; <a href="#t84.2 &#x8FC7;&#x6EE4;&#x5668;"> # </a></h3>
<p>&#x8FC7;&#x6EE4;&#x5668;&#x662F;&#x4E00;&#x4E9B;&#x53EF;&#x4EE5;&#x6267;&#x884C;&#x53D8;&#x91CF;&#x7684;&#x51FD;&#x6570;&#xFF0C;&#x901A;&#x8FC7;&#x7BA1;&#x9053;&#x64CD;&#x4F5C;&#x7B26; (|) &#x8C03;&#x7528;&#xFF0C;&#x5E76;&#x53EF;&#x63A5;&#x53D7;&#x53C2;&#x6570;&#x3002;</p>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> nunjucks=<span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;nunjucks&apos;</span>);
nunjucks.configure({<span class="hljs-attr">autoescape</span>: <span class="hljs-literal">true</span>});
<span class="hljs-keyword">let</span> ret=nunjucks.renderString(<span class="hljs-string">`
{{ names | join(&quot;,&quot;) }}
`</span>,{<span class="hljs-attr">names</span>: [<span class="hljs-string">&apos;name1&apos;</span>,<span class="hljs-string">&apos;name2&apos;</span>]});
<span class="hljs-built_in">console</span>.log(ret);

<span class="hljs-keyword">let</span> ret2=nunjucks.renderString(<span class="hljs-string">`
{{word| replace(&quot;world&quot;, &quot;there&quot;) | capitalize}}
`</span>,{<span class="hljs-attr">word</span>:<span class="hljs-string">&apos;hello world&apos;</span>});
<span class="hljs-built_in">console</span>.log(ret2);
</code></pre>
<h3 id="t94.3 if">4.3 if <a href="#t94.3 if"> # </a></h3>
<p>if &#x4E3A;&#x5206;&#x652F;&#x8BED;&#x53E5;&#xFF0C;&#x4E0E; javascript &#x4E2D;&#x7684; if &#x7C7B;&#x4F3C;&#x3002;</p>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> nunjucks=<span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;nunjucks&apos;</span>);
nunjucks.configure({<span class="hljs-attr">autoescape</span>: <span class="hljs-literal">true</span>});
<span class="hljs-keyword">let</span> ret=nunjucks.renderString(<span class="hljs-string">`
{% if score &gt; 90 %}
 &#x4F18;
{% elseif score&gt;80 %}
 &#x826F;
{% elseif score&gt;70 %}
 &#x4E2D;
{% elseif score &gt;60 %}
 &#x53CA;&#x683C;
{% else %}
 &#x4E0D;&#x53CA;&#x683C;
{% endif %}
`</span>,{<span class="hljs-attr">score</span>:<span class="hljs-number">79</span>});
<span class="hljs-built_in">console</span>.log(ret);
</code></pre>
<h3 id="t104.4 for">4.4 for <a href="#t104.4 for"> # </a></h3>
<p>for &#x53EF;&#x4EE5;&#x904D;&#x5386;&#x6570;&#x7EC4; (arrays) &#x548C;&#x5BF9;&#x8C61; (dictionaries)&#x3002;</p>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> nunjucks=<span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;nunjucks&apos;</span>);
nunjucks.configure({<span class="hljs-attr">autoescape</span>: <span class="hljs-literal">true</span>});
<span class="hljs-keyword">let</span> ret=nunjucks.renderString(<span class="hljs-string">`
&lt;ul&gt;
 {% for item in items %}
   &lt;li&gt;{{loop.index}} {{item.id}}:{{item.name}}&lt;/li&gt;
   {% endfor %}
&lt;/ul&gt;
`</span>,{<span class="hljs-attr">items</span>: [{<span class="hljs-attr">id</span>:<span class="hljs-number">1</span>,<span class="hljs-attr">name</span>:<span class="hljs-string">&apos;zfpx1&apos;</span>},{<span class="hljs-attr">id</span>:<span class="hljs-number">2</span>,<span class="hljs-attr">name</span>:<span class="hljs-string">&apos;zfpx2&apos;</span>}]});
<span class="hljs-built_in">console</span>.log(ret);
</code></pre>
<ul>
<li>loop.index: &#x5F53;&#x524D;&#x5FAA;&#x73AF;&#x6570; (1 indexed)</li>
<li>loop.index0: &#x5F53;&#x524D;&#x5FAA;&#x73AF;&#x6570; (0 indexed)</li>
<li>loop.revindex: &#x5F53;&#x524D;&#x5FAA;&#x73AF;&#x6570;&#xFF0C;&#x4ECE;&#x540E;&#x5F80;&#x524D; (1 indexed)</li>
<li>loop.revindex0: &#x5F53;&#x524D;&#x5FAA;&#x73AF;&#x6570;&#xFF0C;&#x4ECE;&#x540E;&#x5F80;&#x524D; (0 based)</li>
<li>loop.first: &#x662F;&#x5426;&#x7B2C;&#x4E00;&#x4E2A;</li>
<li>loop.last: &#x662F;&#x5426;&#x6700;&#x540E;&#x4E00;&#x4E2A;</li>
<li>loop.length: &#x603B;&#x6570;</li>
</ul>
<h3 id="t114.5 &#x6A21;&#x677F;&#x7EE7;&#x627F;">4.5 &#x6A21;&#x677F;&#x7EE7;&#x627F; <a href="#t114.5 &#x6A21;&#x677F;&#x7EE7;&#x627F;"> # </a></h3>
<ul>
<li>&#x6A21;&#x677F;&#x7EE7;&#x627F;&#x53EF;&#x4EE5;&#x8FBE;&#x5230;&#x6A21;&#x677F;&#x590D;&#x7528;&#x7684;&#x6548;&#x679C;&#xFF0C;&#x5F53;&#x5199;&#x4E00;&#x4E2A;&#x6A21;&#x677F;&#x7684;&#x65F6;&#x5019;&#x53EF;&#x4EE5;&#x5B9A;&#x4E49; &quot;blocks&quot;&#xFF0C;&#x5B50;&#x6A21;&#x677F;&#x53EF;&#x4EE5;&#x8986;&#x76D6;&#x4ED6;</li>
<li>&#x540C;&#x65F6;&#x652F;&#x6301;&#x591A;&#x5C42;&#x7EE7;&#x627F;&#x3002;<h4 id="t124.5.1 index.js">4.5.1 index.js <a href="#t124.5.1 index.js"> # </a></h4>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> nunjucks=<span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;nunjucks&apos;</span>);
<span class="hljs-keyword">const</span> path=<span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;path&apos;</span>);
nunjucks.configure(path.resolve(__dirname,<span class="hljs-string">&apos;views&apos;</span>),{<span class="hljs-attr">autoescape</span>:<span class="hljs-literal">true</span>});
<span class="hljs-keyword">let</span> ret2 = nunjucks.render(<span class="hljs-string">&apos;login.html&apos;</span>,{<span class="hljs-attr">name</span>: <span class="hljs-string">&apos;zfpx&apos;</span>});
<span class="hljs-built_in">console</span>.log(ret2);
</code></pre>
<h4 id="t134.5.2 layout.html">4.5.2 layout.html <a href="#t134.5.2 layout.html"> # </a></h4>
<pre><code class="lang-html"><span class="hljs-meta">&lt;!DOCTYPE html&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">html</span> <span class="hljs-attr">lang</span>=<span class="hljs-string">&quot;en&quot;</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">charset</span>=<span class="hljs-string">&quot;UTF-8&quot;</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;viewport&quot;</span> <span class="hljs-attr">content</span>=<span class="hljs-string">&quot;width=device-width, initial-scale=1.0&quot;</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">http-equiv</span>=<span class="hljs-string">&quot;X-UA-Compatible&quot;</span> <span class="hljs-attr">content</span>=<span class="hljs-string">&quot;ie=edge&quot;</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>Document<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span>
  hello {{name}}
<span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span>
</code></pre>
</li>
</ul>
<h4 id="t144.5.3 login.html">4.5.3 login.html <a href="#t144.5.3 login.html"> # </a></h4>
<pre><code class="lang-html">{% extends &quot;layout.html&quot; %}

{% block content %}
      &#x767B;&#x5F55;&#x9875;&#x9762; {{name}}
{% endblock %}
</code></pre>
<h3 id="t154.6. &#x5305;&#x542B;">4.6. &#x5305;&#x542B; <a href="#t154.6. &#x5305;&#x542B;"> # </a></h3>
<p>include &#x53EF;&#x5F15;&#x5165;&#x5176;&#x4ED6;&#x7684;&#x6A21;&#x677F;&#xFF0C;&#x53EF;&#x4EE5;&#x5728;&#x591A;&#x6A21;&#x677F;&#x4E4B;&#x95F4;&#x5171;&#x4EAB;&#x4E00;&#x4E9B;&#x5C0F;&#x6A21;&#x677F;&#xFF0C;&#x5982;&#x679C;&#x67D0;&#x4E2A;&#x6A21;&#x677F;&#x5DF2;&#x4F7F;&#x7528;&#x4E86;&#x7EE7;&#x627F;&#x90A3;&#x4E48; include &#x5C06;&#x4F1A;&#x975E;&#x5E38;&#x6709;&#x7528;&#x3002;</p>
<h4 id="t164.6.1 index.js">4.6.1 index.js <a href="#t164.6.1 index.js"> # </a></h4>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> nunjucks=<span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;nunjucks&apos;</span>);
<span class="hljs-keyword">const</span> path=<span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;path&apos;</span>);
nunjucks.configure(path.resolve(__dirname,<span class="hljs-string">&apos;views&apos;</span>),{<span class="hljs-attr">autoescape</span>:<span class="hljs-literal">true</span>});
<span class="hljs-keyword">let</span> ret2=nunjucks.render(<span class="hljs-string">&apos;items.html&apos;</span>,{<span class="hljs-attr">items</span>: [{<span class="hljs-attr">id</span>:<span class="hljs-number">1</span>,<span class="hljs-attr">name</span>:<span class="hljs-string">&apos;zfpx1&apos;</span>},{<span class="hljs-attr">id</span>:<span class="hljs-number">2</span>,<span class="hljs-attr">name</span>:<span class="hljs-string">&apos;zfpx2&apos;</span>}]});
<span class="hljs-built_in">console</span>.log(ret2);
</code></pre>
<p>items.html</p>
<pre><code class="lang-html">{% extends &quot;layout.html&quot; %}

{% block content %}
      <span class="hljs-tag">&lt;<span class="hljs-name">ul</span>&gt;</span>
          {% for item in items %}
          {% include &quot;item.html&quot; %}
        {% endfor %}  
      <span class="hljs-tag">&lt;/<span class="hljs-name">ul</span>&gt;</span>
{% endblock %}
</code></pre>
<p>item.html</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span>{{item.id}}:{{item.name}}<span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
</code></pre>
<h2 id="t17&#x53C2;&#x8003;&#x8D44;&#x6599;">&#x53C2;&#x8003;&#x8D44;&#x6599; <a href="#t17&#x53C2;&#x8003;&#x8D44;&#x6599;"> # </a></h2>
<ul>
<li><a href="https://mozilla.github.io/nunjucks/cn/getting-started.html">getting-started</a></li>
<li><a href="https://mozilla.github.io/nunjucks/cn/templating.html#part-d0678dc0cc4cc122">nunjucks</a></li>
</ul>

        <div class="copyright">Powered by <a href="https://github.com/jaywcjlove/idoc" target="_blank">idoc</a>. Dependence <a href="https://nodejs.org">Node.js</a> run.</div>
    </div>
    
</div>

<script src="https://cdn.bootcss.com/jquery/3.0.0/jquery.js"></script>
<script>
$('.warpper .page-toc ul ul li a').on('click',function(){
  $('.warpper .page-toc ul ul li a').removeClass('my-active')
  $(this).addClass('my-active')
})
  // if (!$('.understand-me').length) {
  //   var bar = $(window).height() - $('.navbar ').height() - $('.page-toc').position().top;
  //   var count = bar / 26 / 2;
  //   var barHeight = $('.page-toc').outerHeight();
  //   $('.page-toc li').eq(0).children('a').addClass('red');
  //   var arr = [];
  //   $("h1,h2,h3,h4,h5,h6").each(function () {
  //     arr.push($(this).position().top);
  //   });
  //   var timer
  //   function dark() {
  //     clearTimeout(timer)
  //      timer = setTimeout(function () {
  //      var top = Math.abs($('.page-toc > ul').position().top);
  //      var cur = $('.content').scrollTop();
  //      for (var i = arr.length; i >= 0; i--) {
  //        if (arr[i] <= cur) {
  //          break;
  //        }
  //      }
  //      if (i === -1) {
  //        i = 0;
  //      }
  //      $('.page-toc li a').removeClass('red');
  //      $('.page-toc li').eq(i).children('a').addClass('red');
  //      let height = $('.page-toc li').eq(i).position().top-$('.page-toc').height(); // 如果当前的offset出去了 回到中间可好？
  //      $('.page-toc').scrollTop(height+$('.page-toc').height()/2);
  //    },200)
  //   }

  //   $('.content').on('scroll', dark);
  // }
</script>
<style>

    /* ::-webkit-scrollbar{width:14px;}
    ::-webkit-scrollbar-track{background-color:transparent;}
    ::-webkit-scrollbar-thumb{background-color:transparent;}
    ::-webkit-scrollbar-thumb:hover {background-color:transparent}
    ::-webkit-scrollbar-thumb:active {background-color:transparent} */

    .page-toc > ul .red {
        background: #f3f3f3;
        z-index: 1;
        border-left: 3px solid #009a61;
        -webkit-transition: all .2s ease;
        transition: all .2s ease;
        color: #000
    }





</style>
</body>
</html>
